function Slide() {
	this.obj = null;
	this.now = 0;
	this.len = 0;
	this.width = 0;
	this.times = 300;
	this.inter = 3000;
	this.timer = null;
	this.img_ul = null;
	this.img_li = [];
	this.img_img = [];
	this.num_ol = null;
	this.left = 0;
	this.curX = 0;
	this.disX = 0;
	this.intX = 0;
	this.curY = 0;
	this.intY = 0;
	this.tmpFnMove = function () {};
	this.tmpFnEnd = function () {};
	this.tmpFnResize = function () {};
}

Slide.prototype.init = function (opt) {
	var _this = this;
	
	copy (this, opt);
	
	this.obj = opt.obj;
	this.img_ul = this.obj.querySelector('.images');
	this.img_li = this.img_ul.querySelectorAll('li');
	this.img_imgs = this.img_ul.querySelectorAll('img');
	this.num_ol = this.obj.querySelector('.number');
	this.len = this.img_li.length;
	
	this.img_ul.appendChild(this.img_li[0].cloneNode(true));
	this.img_ul.appendChild(this.img_li[this.len - 1].cloneNode(true));
	this.img_li = this.img_ul.querySelectorAll('li');
  
	this.obj.style.display = 'none';
	
	this.img_imgs[0].onload = function () {
		_this.obj.style.display = 'block';
		_this.render.call(_this);
	};
	
	var imgsrc = this.img_imgs[0].src;
	
	if (/\?/.test(imgsrc)) {
		imgsrc += ('&t=' + new Date().getTime());
	} else {
		imgsrc += ('?t=' + new Date().getTime());
	}
	
	this.img_imgs[0].src = imgsrc;
};

Slide.prototype.render = function () {
	var _this = this;
	
	this.draw();
	this.auto();
	
	this.img_ul.addEventListener('webkitTransitionEnd', function () {
		_this.playend.call(_this);
	}, false);
	
	this.img_ul.addEventListener('touchstart', function (ev) {
		_this.start.call(_this, ev);
	}, false);
	
	this.tmpFnResize = function () {
		_this.draw.call(_this);
	};
	
	bindEvent(window, 'resize', this.tmpFnResize);
};

Slide.prototype.draw = function () {
	this.width = this.obj.offsetWidth;
	this.obj.style.WebkitTransform = 'translateZ(0)';
	this.img_ul.style.width = this.width * this.img_li.length + 'px';
	this.img_ul.style.WebkitTransform = 'translate3d(0, 0, 0)';
	this.img_ul.style.WebkitBackfaceVisibility = 'hidden';
	this.img_ul.style.WebkitTransition = '0ms';

	for (var i = 0, len = this.img_li.length; i < len; i++) {
		this.img_li[i].style.width = this.width + 'px';
		if (i == 0) this.obj.style.height = this.img_imgs[0].height + 'px';
    if (i == len - 1) {
			this.img_li[i].style.position = 'relative';
			this.img_li[i].style.left = -this.img_ul.offsetWidth + 'px';
		}	
	}
	
	this.num_ol.innerHTML = '';
	
	for (var i = 0; i < this.len; i++) {
		if (i == this.now) {
			this.num_ol.innerHTML += '<li class="on"><a href="javascript:;">' + (i + 1) + '</a></li>';
		} else {
			this.num_ol.innerHTML += '<li><a href="javascript:;">' + (i + 1) + '</a></li>';
		}
	}
	
	this.num_ol.style.marginLeft = -(this.num_ol.offsetWidth / 2) + 'px';
	
	this.num_li = this.num_ol.querySelectorAll('li');
};

Slide.prototype.start = function (ev) {
	var _this = this;
	
	clearInterval(this.timer);
	this.timer = null;
	
	var touch = event.targetTouches[0];
	
	var intL = this.left;

	this.disX = touch.pageX - intL;
	this.intX = touch.pageX;
	this.curX = 0;
	this.intY = touch.pageY;
	
	this.img_ul.style.WebkitTransition = '0ms';
	
	this.tmpFnMove = function (ev) {
		_this.move.call(_this, ev);
	};
	
	this.tmpFnEnd = function (ev) {
		_this.end.call(_this, ev);
	};
	
	document.addEventListener('touchmove', this.tmpFnMove, false);
	document.addEventListener('touchend', this.tmpFnEnd, false);
	
	//ev.preventDefault();
};

Slide.prototype.move = function (ev) {
	var touch = event.targetTouches[0];
	
	this.curX = touch.pageX;
	this.left = this.curX - this.disX;
	
	this.curY = touch.pageY;
	
	if (Math.abs(this.curY - this.intY) > 10) {
		//document.documentElement.scrollTop = document.body.scrollTop = getScroll() + this.intY - this.curY;
	} else {
		this.img_ul.style.WebkitTransform = 'translate3d(' + this.left + 'px, 0, 0)';
	}
};

Slide.prototype.end = function (ev) {
	if (this.curX == 0) {
		var target = ev.srcElement;
		window.location.href = target.parentNode.href;
	} else if (Math.abs(this.intX - this.curX) > 50) {
		if (this.intX < this.curX) {
			this.now--;
		} else {
			this.now++;
		}
		
		this.play();
	  this.auto();
	} else {
		this.play();
	  this.auto();
	}
	
	document.removeEventListener('touchmove', this.tmpFnMove, false);
	document.removeEventListener('touchend', this.tmpFnEnd, false);
};

Slide.prototype.auto = function (flag) {
	var _this = this;
	
	this.timer = setInterval(function () {
		!flag && _this.now++;
		_this.play.call(_this);
	}, this.inter);
};

Slide.prototype.play = function () {
	if (this.now > this.len) this.now = 1;
	if (this.now < -1) this.now = this.len - 1;
	this.left = -this.now * this.width;
	this.img_ul.style.WebkitTransition = this.times + 'ms';
	this.img_ul.style.WebkitTransform = 'translate3d(' + this.left + 'px, 0px, 0px)';
};

Slide.prototype.playend = function () {
	this.img_ul.style.WebkitTransition = '0ms';
	
	if (this.now == this.len) {
		this.now = 0;
		this.left = 0;
		this.img_ul.style.WebkitTransform = 'translate3d(' + this.left + ', 0, 0)';
	}
	
	if (this.now == -1) {
		this.now = this.len - 1;
		this.left = -this.now * this.width;
		this.img_ul.style.WebkitTransform = 'translate3d(' + this.left + ', 0, 0)';
	}

	for (var i = 0; i < this.len; i++) {
		if (i == this.now) {
			this.num_li[i].className = 'on';
		} else {
			this.num_li[i].className = '';
		}
	}
};

function copy (obj1, obj2) {
	for (var attr in obj2) if (obj2.hasOwnProperty(attr)) obj1[attr] = obj2[attr];
}

function bindEvent(obj, ev, fn) {
	if (obj.attachEvent) obj.attachEvent('on' + ev, fn);
	else obj.addEventListener(ev, fn, false);	
}

function getScroll() {
  return document.documentElement.scrollTop || document.body.scrollTop;    
}